package ar.com.solidussource.howmuchmobile.activities;

import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.widget.*;
import ar.com.solidussource.howmuchmobile.R;
import ar.com.solidussource.howmuchmobile.activities.filter.CurrentFilter;
import ar.com.solidussource.howmuchmobile.activities.generic.PaginatedListActivity;
import ar.com.solidussource.howmuchmobile.activities.maps.HowMuchMapActivity;
import ar.com.solidussource.howmuchmobile.common.entities.Product;
import ar.com.solidussource.howmuchmobile.common.exception.ServiceException;
import ar.com.solidussource.howmuchmobile.services.ProductRestService;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 07/11/12
 * Time: 16:51
 * To change this template use File | Settings | File Templates.
 */
public class ProductListActivity extends PaginatedListActivity<Product> {

    @Override
    public String getViewTitle() {
        return "Productos";
    }

    @Override
    public void serviceCall() throws ServiceException {
        Integer productsubcategoryId = getIntent().getExtras().getInt("productsubcategoryId");

        ProductRestService productRestService = new ProductRestService();
        data.addAll(productRestService.getProducts(productsubcategoryId, pageNumber, size));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Alternativa 1
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_product, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.MnuOpc1:
                Intent intent = new Intent(ProductListActivity.this, HowMuchMapActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean isForContextMenu() {
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_ctx_product, menu);
    }

    public boolean onContextItemSelected(MenuItem item) {

        final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Product product = data.get(info.position);

        ImageView imageView = (ImageView) info.targetView.findViewById(R.id.arrowup);
        switch (item.getItemId()) {
            case R.id.CtxLblAgregar:
                CurrentFilter.addProduct(product);
                imageView.setImageResource(R.drawable.check);

                Toast toast1 = Toast.makeText(getApplicationContext(), "Producto Agregado!", Toast.LENGTH_SHORT);
                toast1.show();
                break;

            case R.id.CtxLblEliminar:
                CurrentFilter.removeProduct(product);
                imageView.setImageResource(R.drawable.arrowup1);

                Toast toast2 = Toast.makeText(getApplicationContext(), "Producto Eliminado!", Toast.LENGTH_SHORT);
                toast2.show();
                break;

        }

        return super.onContextItemSelected(item);
    }

    class ProductListArrayAdapter extends BaseAdapter {

        private Activity mContext;

        ProductListArrayAdapter(Activity context) {
            mContext = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = mContext.getLayoutInflater();
            View item = inflater.inflate(R.layout.list_gen_row, null);

            TextView lblTitulo = (TextView)item.findViewById(R.id.row);
            lblTitulo.setText(data.get(position).getName());

            if (CurrentFilter.getProducts().contains(data.get(position))) {
                ImageView img = (ImageView) item.findViewById(R.id.arrowup);
                img.setImageResource(R.drawable.check);
            }

            return(item);
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return data.get(i).getId();
        }

    }

    class ProductListOnItemClickListener implements AdapterView.OnItemClickListener {

        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            openContextMenu(v);
        }
    }

    @Override
    public BaseAdapter getListAdapter(Activity context) {
        return new ProductListArrayAdapter(context);
    }

    @Override
    public AdapterView.OnItemClickListener getListOnItemClickListener() {
        return new ProductListOnItemClickListener();
    }
}